package guowei.guliorder.order.controller;

import java.util.Arrays;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import guowei.guliorder.order.entity.OrderEntity;
import guowei.guliorder.order.service.OrderService;
import guowei.common.utils.PageUtils;
import guowei.common.utils.R;



/**
 * 订单
 *
 * @author guoguo
 * @email 1573746397@qq.com
 * @date 2023-05-21 12:20:59
 */
@RestController
@RequestMapping("/order/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 根据登录用户id 查询到所有订单，并分页查询订单的所有商品
     * @param params
     * @return
     */
    @PostMapping("/Orders/ItemList")
    public R getOrdersByMemberId(@RequestBody Map<String, Object> params){

        PageUtils pageUtils = orderService.getOrdersByMemberId(params);
        return R.ok().put("page",pageUtils);
    }

    /**
     * 远程调用，用于判断消息是否需要回滚
     * @param orderSn
     * @return
     */
    @RequestMapping("/getByOrderSn/{orderSn}")
    public R getOrderByOrderSn(@PathVariable("orderSn") String orderSn){
        OrderEntity order = orderService.getByOrderSn(orderSn);
        return R.ok().put("order",order);
    }
    /**
     * 列表
     */
    @GetMapping("/getById/{id}")
    public R getOneById(@PathVariable("id") Integer id){
        OrderEntity orderEntity = orderService.getById(id);
        orderEntity.setBillContent("满一百减10");
        return R.ok().put("order",orderEntity);
    }
    @RequestMapping("/list")
    //@RequiresPermissions("order:order:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("order:order:info")
    public R info(@PathVariable("id") Long id){
		OrderEntity order = orderService.getById(id);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("order:order:save")
    public R save(@RequestBody OrderEntity order){
		orderService.save(order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("order:order:update")
    public R update(@RequestBody OrderEntity order){
		orderService.updateById(order);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("order:order:delete")
    public R delete(@RequestBody Long[] ids){
		orderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
